/*<!--引入jwt-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.3</version>
</dependency>*/

package com.easy.common.util;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTValidator;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JWTUtil {
	/**
	 * 签名每次重启服务签名都会动态生成
	 */
	public static final String SECRET_KEY = "Easy";

	private static final long EXPIRATION = 86400000; // 24小时
	/**
	 * 生成Token的方法
	 * @param username
	 * @param roles
	 * @return
	 */
	public static String generateToken(String username, List<String> roles) {
		System.out.println(SECRET_KEY);
		return JWT.create().setExpiresAt(new Date(System.currentTimeMillis()+EXPIRATION))
				.setKey(SECRET_KEY.getBytes())
				.sign();
	}
	//验证token
//	public static Claims parseToken(String token) {
//		return Jwts.parser()
//				.setSigningKey(SECRET_KEY)
//				.parseClaimsJws(token)
//				.getBody();
//	}
	public static Object parseToken(String token) {
		JWT jwt=cn.hutool.jwt.JWTUtil.parseToken(token);
		System.out.println(jwt);
		return jwt;
	}
	public static boolean verify(String token){
		if(!cn.hutool.jwt.JWTUtil.verify(token, SECRET_KEY.getBytes())){
			throw new RuntimeException("签名异常");
		}
		try {
			JWTValidator.of(token).validateDate();
		}catch (Exception e){
			throw new RuntimeException("签名过期");
		}
		return true;
	}

	public static void main(String[] args) {
		Map map=new HashMap();
		map.put("name","zhangsan");
//		String token=generateToken(map);
//		System.out.println(token);
	}
}
